
<div class="section" id="khuvuclist">

	<div class="section-title">
    	Quản lý lập phiếu phân phối báo
    </div>
    
    <div class="section-content padding1">
    	<div class="button right">
            <a class="button save" onclick="save()">Lưu</a>
			<a class="button cancel" href="?route=deliman/phieuphanphoi">Bỏ qua</a>
            
        </div>
        <div class="clearer">^&nbsp;</div>
    	<form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
        
        	<input type="hidden" name="phieuphanphoiid" value="<?php echo $item['phieuphanphoiid']?>">
        	<div id="error" class="error" style="display:none"></div>
            
        	
            	
            <div id="fragment-thongtin">
            	<table class="table-form">
                	<!--<tr>
                    	<td><label>Mã phiếu:</label></td>
                        <td><input type="text" name="maphieuphanphoi" value="<?php echo $item['maphieuphanphoi']?>" class="text <?php echo $class?>"  size=60 <?php echo $readonly?>/></td>
                    </tr>-->
                    
                    <!--<tr>
                    	<td><label>Ngày lập:</label></td>
                        <td><input type="text" name="ngaylap" value="<?php echo $item['ngaylap']?>" class="text ben-datepicker"/></td>
                    </tr>-->
                    <tr>
                    	<td><label>Loại báo:</label></td>
                        <td>
                        	<select id="loaibaoid" name="loaibaoid">
                            	<option value="">Chọn loại báo</option>
                                <?php foreach($loaibaos as $val){ ?>
                                <option value="<?php echo $val['loaibaoid']?>"><?php echo $val['tenloaibao']?></option>
                                <?php } ?>
                            </select>
                            <label>Kỳ báo:</label>
                     		<select id="kybaoid" name="kybaoid">
                            	
                            </select>
                            <label>Ngày phát hành:</label>
                     		<input type="text" id="ngayphathanh" name="ngayphathanh" class="text" readonly="readonly"/>       
                        </td>
                    </tr>
                    <tr>
                    	<td><label>Tổng chỉ tiêu:</label></td>
                        <td><input type="text" id="chitieu" name="chitieu" value="<?php echo $item['chitieu']?>" class="text number"  size=60 /></td>
                    </tr>
                    <tr>
                    	<td><label>Ghi chú:</label></td>
                        <td><textarea name="ghichu" style="width:50%"><?php echo $item['ghichu']?></textarea></td>
                    </tr>
                </table>
                
            </div>
            <div class="khuvuc treeindex">

                  <link href="<?php echo DIR_VIEW?>css/jquery.treeTable.css" rel="stylesheet" type="text/css" />
                  <script type="text/javascript" src="<?php echo DIR_VIEW?>js/jquery.treeTable.js"></script>
                  <script type="text/javascript">
                  $(document).ready(function() {
                    // TODO Fix issue with multiple treeTables on one page, each with different options
                    // Moving the #example3 treeeTable call down will break other treeTables that are expandable...
                    $(".example").treeTable();
                  });
                  
                  </script>
                  <table class="example data-table table-form">
                    <thead>
                        <tr class="tr-head">
                            
                            
                            <th>Khu vực</th>
                            <th width="20%">Chỉ tiêu</th>
                                                             
                        </tr>
                    </thead>
                    <tbody>
        
        <?php
        	
            foreach($khuvucs as $khuvuc)
            {
        ?>
                    <tr  id="<?php echo $khuvuc['eid']?>" class="<?php echo $khuvuc['class']?>">
                        
                        
                        <td>
                        	<?php echo $khuvuc['tab']?>
                            <input  type="hidden" name="khuvucid[<?php echo $khuvuc['khuvucid']?>]" value="<?php echo $khuvuc['khuvucid']?>" >
                            <input type="hidden" name="id[<?php echo $khuvuc['khuvucid']?>]" value="<?php echo $khuvuc['ctphanphoi']['id']?>" />
                        	<?php echo $khuvuc['tenkhuvuc']?>
                        </td>
                        
                        <td class="number">
                        	<input type="text" class="chitieu<?php echo $khuvuc['khuvuccha']?> text number" name="chitieukv[<?php echo $khuvuc['khuvucid']?>]" value="<?php echo $khuvuc['ctphanphoi']['chitieu']?>" size="20"/>
                        </td>
                        
                    </tr>
        <?php
            }
        ?>
                        
                                                    
                </tbody>
                </table>
                
             
            </div> 
            <div>
            	<p class="right">
                	Tổng số lượng đã phân phối: <span id="soluongpp" style="color:#F00"></span>/<span id="tongchitieu"></span>
                </p>
                <div class="clearer">^&nbsp;</div>
            </div>  
                
            
            
        </form>
    
    </div>
    
</div>

<script language="javascript">
$('#loaibaoid').change(function(e) {
    $('#kybaoid').load('?route=deliman/phieuphanphoi/getKyBaos&loaibaoid='+$('#loaibaoid').val()+"&kybaoid=<?php echo $item['kybaoid']?>",function(){
		$('#kybaoid').val("<?php echo $item['kybaoid']?>");	
		$('#kybaoid').change();
	});
});

$('#kybaoid').change(function(e) {
	if(this.value!="")
		$.getJSON("?route=deliman/kybao/getKyBao",
			{
				col:'kybaoid',
				val:this.value
			},
			function(data){
			if(data.kybaos.length>0)
			{
				$('#ngayphathanh').val(data.kybaos[0].ngayphathanh);
				if("<?php echo $item['phieuphanphoiid']?>" == "")
					$('#chitieu').val(formateNumber(data.kybaos[0].soluongin)).change();
				
			}
			
		});
});
$(document).ready(function(e) {
    $('#loaibaoid').val("<?php echo $item['loaibaoid']?>");
	$('#loaibaoid').change();
	$('#chitieu').change();
	$('.chitieu0').keyup();
});
$('#chitieu').change(function(e) {
    $('#tongchitieu').html($(this).val());
});

$('.chitieu0').keyup(function(e) {
	//alert('fdf');
	var sum = 0;
    $('.chitieu0').each(function(index, element) {
        sum += Number(this.value.replace(/,/g,""));
		
    });
	if( sum > Number($('#tongchitieu').html().replace(",","")))
	{
		this.value ='';	
		alert('Bạn nhập số lượng phân phối vượt quá tổng chỉ tiêu');
		//$('.chitieu0').keyup();
		
	}
	else
	{
		$('#soluongpp').html(formateNumber(sum));
	}
});
<!--thực hiện chức năng save dữ liệu-->
function save()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	
	$.post("?route=deliman/phieuphanphoi/save", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				window.location = "?route=deliman/phieuphanphoi";
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}
</script>
